package tools

import java.sql.Timestamp
import java.text.SimpleDateFormat
import java.util.{Calendar, Date}

/**
  * @author chensi
  */
object TimeTools {

  private val sdf = new SimpleDateFormat("yyyyMMdd")

  /**
    * 获取当前时间是当月的第几周
    * @return 2018084
    */
  def getWeek() = {
    val cal = Calendar.getInstance()
    cal.setFirstDayOfWeek(Calendar.MONDAY)
    Integer.parseInt(sdf.format(cal.getTime))/100*10 + cal.get(Calendar.WEEK_OF_MONTH)
  }

  /**
    * 获取当前时间是第几个月
    * @return 201808
    */
  def getMonth() = {
    Integer.parseInt(sdf.format(new Date()))/100
  }

  /**
    *获取该时间是当月的第几天
    * @param ts
    * @return
    */
  def getDayOfMonth(ts:Timestamp) = {
    val cal = Calendar.getInstance()
    cal.setFirstDayOfWeek(Calendar.MONDAY)
    cal.setTime(new Date(ts.getTime))
    cal.get(Calendar.DAY_OF_MONTH)
  }

  def getDayInfo(ts:Timestamp) = {
    val cal = Calendar.getInstance()
    cal.setFirstDayOfWeek(Calendar.MONDAY)
    cal.setTime(new Date(ts.getTime))
    (cal.get(Calendar.WEEK_OF_MONTH),cal.get(Calendar.DAY_OF_MONTH))
  }

  def getDayOfWeek(ts:Timestamp)={
    val cal = Calendar.getInstance()
    cal.setFirstDayOfWeek(Calendar.MONDAY)
    cal.setTime(new Date(ts.getTime))
    cal.get(Calendar.DAY_OF_WEEK)
  }

  /**
    * @param ts timestamp
    * @return egg:20180820
    */
  def formatTs(ts:Timestamp)={
    Integer.parseInt(sdf.format(new Date(ts.getTime)))
  }

  /**
    * 判断recordT与stateT是否在同一个月份
    * @param recordT
    * @param stateT
    * @return
    */
  def isSameMonth(recordT: Timestamp, stateT: Int): Boolean = {
    stateT/100 == formatTs(recordT)/100
  }

  def weekOfMonth(time:Timestamp): Int ={
    val c = Calendar.getInstance()
    1
  }

  def main(args: Array[String]): Unit = {
    println(getWeek())
  }
}
